package com.exteam.common.base;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.SeekBar;

import com.exteam.common.view.dialog.LoadingDialog;
import com.exteam.common.view.dialog.MessageDialog;


/**
 * 所有Controller父类
 *
 * @author sean.yanghy
 * @version v1.0
 * @created 2014-11-20 13:56:23
 */

public abstract class BaseController {

    protected Handler mainHandler; // 主Handler
    protected LoadingDialog loadingDialog;
    protected MessageDialog messageDialog;

    protected BaseActivity currAct; // 当前Activity
    protected BaseFragment currFrag; // 当前Fragment
    protected View currFragView;
    protected BaseFragmentActivity currFragAct; // 当前FragmentActivity

    public BaseController(BaseActivity act) {
        this.currAct = act;
        this.loadingDialog = act.getLoadingDialog();
        this.messageDialog = act.getMessageDialog();
        this.mainHandler = act.getMainHandler();
        initView();
        initEventListener();

    }

    public BaseController(BaseFragmentActivity act) {
        this.currFragAct = act;
        this.loadingDialog = act.getLoadingDialog();
        this.messageDialog = act.getMessageDialog();
        this.mainHandler = act.getMainHandler();

        initView();
        initEventListener();
    }

    public BaseController(BaseFragmentActivity act, BaseFragment frag,
                          View currFragView) {
        this.currFragAct = act;
        this.currFrag = frag;
        this.currFragView = currFragView;
        this.mainHandler = act.getMainHandler();
        this.loadingDialog = act.getLoadingDialog();
        this.messageDialog = act.getMessageDialog();
        initView();
        initEventListener();

    }

    /**
     * 初始化View
     */
    protected abstract void initView();

    /**
     * 初始化View事件
     */
    protected abstract void initEventListener();

    /**
     * 处理主线程消息
     */
    public void processMainHandleMessage(Message msg) {

    }

    /**
     * 处理事件
     */
    public abstract void onViewClick(View v);


    /**
     * 封装发送消息方法
     */
    public void sendMainHandlerMessage(int what, Object obj) {
        Message msg = mainHandler.obtainMessage();
        msg.what = what;
        msg.obj = obj;
        mainHandler.sendMessage(msg);
    }

    public void onRestart() {

    }

    public void onStart() {

    }

    public void onPause() {

    }

    public void onStop() {

    }

    public void onResume() {

    }

    public void onDestroy() {

    }

    // public abstract void onConfigurationChanged(Configuration newConfig) ;

    /**
     * 清空引用
     */
    public void clear() {
        mainHandler = null;
        loadingDialog = null;
        messageDialog = null;
        currAct = null;
        currFrag = null;
        currFragAct = null;
    }

    public void onConfigurationChanged(Configuration newConfig) {

    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    }

}
